from Clustering import Clustering
from distance import euclid_dis
from analysis import sequential, hierarchical, k_means, dunn, davies_bouldin, random_clu, inner_sum_squares
from draw import draw_clustering, draw_curve

if __name__ == "__main__":
    samples = [[5,2],[1,2],[2,1],[6,2],[1,1],[3,1],[7,-1],[5,-1]]

    '''
    作业一 题3.
    '''



    '''
---------顺序聚类分析-----------------
    '''
    s = sequential(sp=samples, th=3, M=5, get_dis=euclid_dis)
    print("顺序聚类分析结果为: ", s)
    print("dunn指数:  ",dunn(clu=s, get_dis=euclid_dis))
    print("Davies-Bouldin指数: ", davies_bouldin(clu=s))
    draw_clustering(s)



    ''' 
---------谱系聚类分析-----------------
    '''
    h = hierarchical(sp=samples, th=3, M=5, get_dis=euclid_dis)
    print("谱系聚类分析结果为: ",h)
    print("dunn指数:  ",dunn(h,euclid_dis))
    print("Davies-Bouldin指数: ",davies_bouldin(h))
    draw_clustering(h)



    '''
---------k-means聚类分析-----------------
    '''
    K = 3
    clu = random_clu(samples=samples, K=K)
    k = k_means(sp=samples, clu=clu, get_dis=euclid_dis)
    print("k-means聚类分析结果为: ",k)
    print("dunn指数:  ",dunn(k,euclid_dis))
    print("Davies-Bouldin指数: ",davies_bouldin(k))
    draw_clustering(k)


    '''
    作业一 题4.
    '''

    # 从文件中读取样本
    sp = []
    with open('samples.txt') as f:
        sp = [[eval(s.split()[0]), eval(s.split()[1])] for s in f.readlines()]
    res = []
    for K in range(1,10):
        k = k_means(sp,random_clu(sp,K))
        res.append(inner_sum_squares(k)) 
    print(res)
    
    #  绘制肘型图
    draw_curve(x_label='k',y_label='Within Gourps Sum of Square',x=range(1,len(res)+1), y=res)

    # 绘制聚类结果 
    clu = random_clu(samples=sp, K=4)
    k = k_means(sp=sp, clu=clu, get_dis=euclid_dis)
    draw_clustering(k)
